package com.teamone.etl.apimanager.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.teamone.etl.apimanager.dao.ApiChargeDao;
import com.teamone.etl.apimanager.dao.ApiSourceDao;
import com.teamone.etl.apimanager.entity.ApiCharge;
import com.teamone.etl.apimanager.entity.ApiSource;
import com.teamone.etl.apimanager.request.BaseQuery;
import com.teamone.etl.apimanager.service.ApiChargeService;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 接口计费表(ApiCharge)表服务实现类
 *
 * @author makejava
 * @since 2021-01-20 18:32:04
 */
@Service("apiChargeService")
@Transactional(rollbackFor = Exception.class)
public class ApiChargeServiceImpl extends ServiceImpl<ApiChargeDao, ApiCharge> implements ApiChargeService {

    @Resource
    private ApiSourceDao apiSourceDao;

    @Override
    public PageInfo<ApiCharge> selectAll(BaseQuery baseQuery) {
        PageHelper.startPage(baseQuery.getPageNum(),baseQuery.getPageSize());
        List<ApiCharge> apiCharges=baseMapper.selectAll();
        List<ApiSource> apiSources = apiSourceDao.selectAll(null);
        apiCharges.forEach(apiCharge -> apiSources.forEach(apiSource -> {
            if(apiCharge.getApiSourceId().equals(apiSource.getId())){
                apiCharge.setApiSourceName(apiSource.getName());
            }
        }));
        return new PageInfo<>(apiCharges);
    }

    @Override
    public void saveOne(ApiCharge apiCharge) {
        baseMapper.saveOne(apiCharge);
    }

    @Override
    public void logicDeleteById(Integer id) {
        baseMapper.logicDeleteById(id);
    }

    @Override
    public void updateOne(ApiCharge apiCharge) {
        baseMapper.updateOne(apiCharge);
    }
}